////////////////////////////////////////////////////////////////////////////////
//
//	Original file Name: ScreenController.java
//	Originating Author: JWhitefield
//	
//	Description: Used to control the interfaces weather that be the game screen or the menus 
//
////////////////////////////////////////////////////////////////////////////////

package com.jjw.game.screen;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import com.jjw.game.ITickable;

/**
 * Used to control the interfaces weather that be the game screen or the menus
 * 
 * @author Jon Whitefield
 *
 */
public enum ScreenController implements ITickable {
	INSTANCE;

	//--------------------------------------------------------------------------
	//
	//Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	// Public
	//----------------------------------
	
	//----------------------------------
	//  Getters / Setters
	//----------------------------------
	
	//----------------------------------
	// Private
	//----------------------------------
	
	/**
	 * Default serial version uid
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * List of the screens registered to the screen controller
	 */
	private List<Screen> registeredScreens;

	private GameRenderer gr;
	
	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------
		
	//----------------------------------
	// Public
	//----------------------------------
	
	private ScreenController() {
		
		this.gr = new GameRenderer(this);
	}
	
	/**
	 * Renders the necessary 
	 */
	public void render(Graphics g) {
		
		//Sets the graphics of the registered screens
		this.setGraphics(g);
		
		this.gr.render();
	}
		
	/**
	 * Registers the screen with the screen controller to 
	 * receive the latest graphic object to draw with
	 * 
	 * @param screen
	 */
	public void registerScreen( Screen screen ) {
		
		//If we don't have a registered screens list create one
		if( this.registeredScreens == null )
			this.registeredScreens = new ArrayList<Screen>();
		
		//If the screen is null we can't register it
		if( screen == null )
			return;
		
		//If the screen is already in the list then don't do anything
		if( this.registeredScreens.contains( screen ) )
			return;
		
		//Add the screen to the registered screen list
		this.registeredScreens.add( screen );
	}
	
	/**
	 * Unregisters the screen from the screen's list
	 * so it no longer receives graphics updates
	 * 
	 * @param screen
	 */
	public void unregisterScreen( Screen screen ) {
		
		//If we don't have a registered screens list create one
		if( this.registeredScreens == null )
			return;
		
		//If the screen is null we can't remove it
		if( screen == null )
			return;
		
		//Check that the screen is registered
		if( this.registeredScreens.contains( screen ) )
			this.registeredScreens.remove(screen);
	}
	
	//----------------------------------
	// Private
	//----------------------------------
	
	/**
	 * Sets the registered screens graphics objects
	 */
	private void setGraphics( Graphics g ) {
		
		//If we don't have a list of screens or no graphics object stop here
		if( this.registeredScreens == null ||  g == null )
			return;
		
		//Loop through all the registered screens setting the graphics object
		for( Screen screen : this.registeredScreens )
			screen.setGraphics(g);
	}


	//----------------------------------
	// Overridden / Implemented
	//----------------------------------
	
	@Override
	public void tick() {
		
		this.gr.tick();
		
	}
	
}
